package com.sinosun.autotestmachine.tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.parser.ContentMaker;
import com.sinosun.mstplib.SafeMode;

import android.util.Log;

public enum EnvTag {
	SHANGHAI(0, "production"),	//上海
	NEIWANG(1, "neiwang"),		//内网
	ZHIWEN(2, "fingerprint"),		//指纹
	SHENGZHEN(3, "shenzhen"),	//深圳
	SHAHE(4, "sandbox"),		//沙盒
	HEIHE(5, "heihe");		//黑盒
	
	
	private int value;
	private String valueStr;
	
	private String appId;
	private String serverIp;
	private String port;
	private String tokenKeyFileName;
	private String tokenPassword;
	private String tokenKey;
	private int encrypt;
	private String autoTestServerHost;
	private int autoTestServerPort;
	private int taskReadMode = -1;			//0:读取本地用例信息；1：读取网络用例信息
	private Map<String, String> users = new HashMap<String, String>();
	private Map<String, String> msgContent = new HashMap<String, String>();
	private Map<String, String> fileContent = new HashMap<String, String>();
	private Map<String, String> groupContent = new HashMap<String, String>();
	private Map<String, String> notifyContent = new HashMap<String, String>();
	
	EnvTag(int value, String valueStr) {
		this.value = value;
		this.valueStr = valueStr;
		setUp(valueStr);
		
		ContentMaker.cpAssertToFiles(TestConfig.MODE_JSON_NAME, App.getInstance().getConfigPath() + TestConfig.MODE_JSON_NAME);
		File file = new File(App.getInstance().getConfigPath() + TestConfig.MODE_JSON_NAME);
		
		try {
			InputStream configStream = new FileInputStream(file);
			String modeJson = inputStreamToString(configStream);
			JSONObject jo = new JSONObject(modeJson);
			if (jo.has(TestConfig.TASK_READ_MODE)) {
				taskReadMode = jo.getInt(TestConfig.TASK_READ_MODE);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public String toString() {
		return valueStr;
	}
	
	private void setUp(String fileName) {
		try {
			String confPath = fileName + ".json";
			ContentMaker.cpAssertToFiles(confPath);
			File file = new File(App.getInstance().getTestConfigPath() + confPath);
	        InputStream configStream = new FileInputStream(file);                  //创建输入流 
			String configInfo = inputStreamToString(configStream);
			JSONObject configJson = new JSONObject(configInfo);
			
			if (configJson.has(TestConfig.AUTO_TEST_SERVER_HOST)) {
				autoTestServerHost = configJson.getString(TestConfig.AUTO_TEST_SERVER_HOST);
			}
			if (configJson.has(TestConfig.AUTO_TEST_SERVER_PORT)) {
				autoTestServerPort = configJson.getInt(TestConfig.AUTO_TEST_SERVER_PORT);
			}
			if (configJson.has(TestConfig.APP_ID)) {
				appId = configJson.getString(TestConfig.APP_ID);
			}
			if (configJson.has(TestConfig.RS_HOST)) {
				serverIp = configJson.getString(TestConfig.RS_HOST);
			}
			if (configJson.has(TestConfig.RS_PORT)) {
				port = configJson.getString(TestConfig.RS_PORT);
			}
			if (configJson.has(TestConfig.IBC_APP_PRIVATE_FILENAME)) {
				tokenKeyFileName = configJson.getString(TestConfig.IBC_APP_PRIVATE_FILENAME);
			}
			if (configJson.has(TestConfig.IBC_APP_PASSWORD)) {
				tokenPassword = configJson.getString(TestConfig.IBC_APP_PASSWORD);
			}
			if (configJson.has(TestConfig.IBC_PARA_FILENAME)) {
				tokenKey = configJson.getString(TestConfig.IBC_PARA_FILENAME);
			}
			if (configJson.has(TestConfig.ENCRYPTION)) {
				String tmp = configJson.getString(TestConfig.ENCRYPTION);
				if (tmp.equals("IBC")) {
					encrypt = SafeMode.AsymmetricCryptoType.ALG_IBC;
				} else {
					encrypt = SafeMode.AsymmetricCryptoType.ALG_SM2;
				}
			}
			if (configJson.has(TestConfig.USERS)) {
				String[] tmp = configJson.getString(TestConfig.USERS).split(",");
				for (int i = 0; i < tmp.length; i++) {
					users.put("user" + (i+1), tmp[i]);
				}
			}
			
			configStream.close();
			readConfigContent();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private Map<String, Object> readFileContentFunc(String filePath) {
		Map<String, Object> fileContentTmp = new HashMap<String, Object>();
		try {
			InputStream msgStream = App.getInstance().getApplicationContext().getAssets().open(filePath);
			String msgInfo = inputStreamToString(msgStream);
			fileContentTmp = JSON.parseObject(msgInfo);
			msgStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileContentTmp;
	}
	
	private void readConfigContent() {
		Map<String, Object> tmpMap = readFileContentFunc("caseconfig/messageContent.txt");
		for (String key : tmpMap.keySet()) {
			msgContent.put(key, tmpMap.get(key).toString());
		}
		
		Map<String, Object> tmpMap1 = readFileContentFunc("caseconfig/fileContent.txt");
		for (String key : tmpMap1.keySet()) {
			fileContent.put(key, tmpMap1.get(key).toString());
		}
		
		Map<String, Object> tmpMap2 = readFileContentFunc("caseconfig/groupParam.txt");
		for (String key : tmpMap2.keySet()) {
			groupContent.put(key, tmpMap2.get(key).toString());
		}
		
		Map<String, Object> tmpMap3 = readFileContentFunc("caseconfig/notifyContent.txt");
		for (String key : tmpMap3.keySet()) {
			notifyContent.put(key, tmpMap3.get(key).toString());
		}
	}
	
	private String inputStreamToString(InputStream inputStream) throws IOException {	//输入流转换成字符串
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int count = -1;
		while((count = inputStream.read(data, 0, 1024)) != -1) {
			outStream.write(data, 0, count);
		}
		data = null;
		String ret = outStream.toString();
		outStream.close();
		return ret;
	}
	
	protected int getAutoTestServerPort() {
		return this.autoTestServerPort;
	}
	
	protected String getAutoTestServerHost() {
		return this.autoTestServerHost;
	}
	
	protected int getTaskReadMode() {
		return this.taskReadMode;
	}

	protected int value() {
		return this.value;
	}
	
	protected String getAppId() {
		return appId;
	}
	
	protected String getServerIp() {
		return serverIp;
	}
	
	protected String getPort() {
		return port;
	}
	
	protected String getTokenKeyFileName() {
		return tokenKeyFileName;
	}
	
	protected String getTokenPassword() {
		return tokenPassword;
	}
	
	protected String getTokenKey() {
		return tokenKey;
	}
	
	protected int getEncrption() {
		return encrypt;
	}
	
	protected Map<String, String> getUsers() {
		return users;
	}
	
	protected Map<String, String> getMsgContent() {
		return msgContent;
	}
	
	protected Map<String, String> getFileContent() {
		return fileContent;
	}
	
	protected Map<String, String> getGroupContent() {
		return groupContent;
	}
	
	protected Map<String, String> getNotifyContent() {
		return notifyContent;
	}
}
